package pl.edu.pw.MidEmu.UI.Controllers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.swing.JButton;

import pl.edu.pw.MidEmu.Base.Controllers.BaseController;
import pl.edu.pw.MidEmu.Common.Consts;
import pl.edu.pw.MidEmu.Common.CopyManager;
import pl.edu.pw.MidEmu.Common.Models.CBRDModel;
import pl.edu.pw.MidEmu.Common.Models.MidasModel;
import pl.edu.pw.MidEmu.Logic.ConfigurationService;
import pl.edu.pw.MidEmu.Logic.ProcessHandler;
import pl.edu.pw.MidEmu.UI.Components.MidasComponent;
import pl.edu.pw.MidEmu.UI.Views.CBRDLoaderView;

/**
 * Controller for loading CBRD component
 * @author Mateusz Starodab
 *
 */
public class CBRDLoaderController extends BaseController<CBRDLoaderView> implements ActionListener
{
	private ConfigurationService _configurationService;

	public CBRDLoaderController()
	{
		_configurationService = new ConfigurationService();
	}

	@Override
	public void actionPerformed(ActionEvent e)
	{
		if (e.getSource() instanceof JButton)
		{
			JButton button = (JButton) e.getSource();
			if (button.getName() == Consts.CBRDLoader_CloseButton)
			{
				getView().dispose();
			}
			else if (button.getName() == Consts.CBRDLoader_StartButton)
			{
				if (getView().validateCBRD())
				{
					MidasModel parent = getView().getParentMidasModel();

					String binDir = getView().getBinDir();

					File newDir = new File(parent.getTemporaryDir() + "/cbrd");
					String newDirPath = "";

					try
					{
						newDirPath = newDir.getCanonicalPath();
					}
					catch (IOException e1)
					{
						e1.printStackTrace();
					}

					CopyManager.copyFiles(binDir, newDirPath);

					String cbrdExecutableFileName = getView().getCDRBExecutableFileName();

					File execFile = new File(cbrdExecutableFileName);

					String cbrId = getView().getCBRid();

					File newExecFile = new File(newDirPath + java.io.File.separatorChar + execFile.getName());
					newExecFile.setExecutable(true);

					if (parent.getIsCentralNode())
						_configurationService.setCBRDDemonConfig(newDirPath, parent.getInterfaceName(), getView().getNodesInterfacesAndIPS(), cbrId, parent.getIpAddress(), getView().getNodesIDsAndIPs(), parent.getIpAddress());
					else
					{
						MidasComponent component = getView().getCentralNodeModel();
						_configurationService.setCBRDDemonConfig(newDirPath, parent.getInterfaceName(), getView().getNodesInterfacesAndIPS(), component.getController().getCBRD().getCBRid(), component.getController().getMidas().getIpAddress(), getView().getNodesIDsAndIPs(), parent.getIpAddress());
					}
					ProcessHandler handler = new ProcessHandler(newExecFile.getAbsolutePath(), "-i " + cbrId + " -q " + parent.getIpAddress() + " -m " + newDirPath + "/" + Consts.CBRD_MappingsConfigFile +" -n " + newDirPath + "/" + Consts.CBRD_NetworkConfigFile + " -e " + newDirPath + "/" + Consts.CBRD_CentralNodeConfigFile + (parent.getIsCentralNode() ? " -c" : "") + " -l logfile", Consts.CBRDLoader_Name);
					handler.start();

					CBRDModel model = new CBRDModel();
					model.setCBRDBinDirLocation(binDir);
					model.setExecutableFileName(cbrdExecutableFileName);
					model.setCBRid(cbrId);

					model.save();

					getView().registerCBRDServer(model, handler);

					getView().dispose();
				}
				else
					getView().reportValidationErrors();
			}
			else if (button.getName() == Consts.CBRDLoader_CBRDBinDirLocationButton)
			{
				getView().showPickupBinDirDialog();
			}
		}
	}
}
